package com.skyline.wizard.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyline.common.bean.Page;
import com.skyline.user.model.User;
import com.skyline.user.type.Sex;
import com.skyline.wizard.searcher.ArticleSearcher;
import com.skyline.wizard.searcher.UserSearcher;
import com.skyline.wo.model.Article;

@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private ArticleSearcher articleSearcher;

	@Autowired
	private UserSearcher userSearcher;

	@Value("${wizard.articlePageSize}")
	private Integer articlePageSize;

	@Value("${wizard.userPageSize}")
	private Integer userPageSize;

	@RequestMapping(value = "/article.html", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> searchArticle(String query, Page page) {
		page.setSize(articlePageSize);
		List<Article> articles = articleSearcher.searchWithDefaultAnalyzer(query, page);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("data", articles);
		return map;
	}

	@RequestMapping(value = "/user.html", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> searchUser(String query, Page page) {
		page.setSize(userPageSize);
		List<User> users = userSearcher.searchWithDefaultAnalyzer(query, page);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("data", users);
		return map;
	}

	@RequestMapping(value = "/user/refine.html", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> searchUserRefine( String query, String province, String city,
			String graduateSchool, Sex sex, Integer lowAge, Integer upAge, Page page) {
		page.setSize(userPageSize);
		List<User> users = userSearcher.searchWithDefaultAnalyzer(query, query, province, city, graduateSchool, sex,
				lowAge, upAge, page);
		System.out.println(query+":"+ query+":"+ province+":"+ city+":"+ graduateSchool+":"+ sex+":"+
				lowAge+":"+ upAge);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("data", users);
		return map;
	}
}
